/**
 * Calculon - A Java chess-engine.
 *
 * Copyright (C) 2008-2009 Barry Smith
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package nl.zoidberg.calculon.analyzer;

import nl.zoidberg.calculon.engine.BitBoard;
import nl.zoidberg.calculon.engine.Bitmaps;
import nl.zoidberg.calculon.model.Piece;

public class BishopMobilityScorer implements PositionScorer {

	public int scorePosition(BitBoard bitBoard) {
		int score = 0;
		score += getScore(bitBoard, Piece.WHITE);
		score -= getScore(bitBoard, Piece.BLACK);
		return score;
	}

	private int getScore(BitBoard bitBoard, byte color) {
		long bishopMap = (bitBoard.getBitmapColor(color) & bitBoard.getBitmapBishops());
		int score = 0;
		
		while(bishopMap != 0) {
			long nextBishop = Long.highestOneBit(bishopMap);
			bishopMap ^= nextBishop;
			int mapIdx = Long.numberOfTrailingZeros(nextBishop);
			long dirs = Bitmaps.diag2Map[mapIdx] & BitBoard.getRankMap((mapIdx>>>3) + (color == Piece.WHITE ? 1 : -1));
			long freeAdvance = dirs & ~bitBoard.getBitmapColor(color);
			score += 75 * Long.bitCount(freeAdvance);
		}

		return score;
	}
}
